//
//  MakeAppointmentViewController.m
//  Showing
//
//  Created by Ralf on 15/4/24.
//  Copyright (c) 2015年 next-software. All rights reserved.
//

#import "MakeAppointmentViewController.h"
#import "XLForm.h"
#import "AppDelegate.h"
#import "AppointmentManager.h"
#import "XLFormViewController+Helper.h"

@interface MakeAppointmentViewController ()
{
    NSDictionary *contact;
    NSDictionary *advertising;
    AppDelegate  *appDelegate;
}
@end

@implementation MakeAppointmentViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"Save-60.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(publishButtonOnTap:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = menuButton;

    [self initBackButton];
}

- (id)initWithData:(NSDictionary *)data
{
    XLFormDescriptor        *form;
    XLFormSectionDescriptor *section;
    XLFormRowDescriptor     *row;

    form = [XLFormDescriptor formDescriptorWithTitle:@""];
    form.assignFirstResponderOnShow = YES;

    section = [XLFormSectionDescriptor formSection];

    [form addFormSection:section];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"assignTo" rowType:XLFormRowDescriptorTypeText];
    row.title = @"老师";
    row.value = data[@"article"][@"contact"][@"alias"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"indexOfSaleTags" rowType:XLFormRowDescriptorTypePhone];
    row.title = @"项目";
    row.value = data[@"article"][@"indexOfSaleTags"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"cellphone" rowType:XLFormRowDescriptorTypePhone];
    row.title = @"联系电话";
    row.value = data[@"article"][@"contatc"][@"cellphone"];
    [row setDisabled:@YES];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startTime" rowType:XLFormRowDescriptorTypeDateTime];
    row.title = @"预约时间";
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    //    row.value = [dateFormatter dateFromString:data[@"article"][@"startTime"]];
    row.value    = [NSDate new];
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"address" rowType:XLFormRowDescriptorTypeEmail];
    row.title    = @"服务地址";
    row.required = YES;
    [section addFormRow:row];

    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"message" rowType:XLFormRowDescriptorTypeTextView];
    row.title    = @"留言";
    row.required = YES;
    [section addFormRow:row];

    return [super initWithForm:form];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)didClickBackAction
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    [self.tableView reloadData];
//    [scrollView endEditing:YES];
//}

#pragma mark - Table view data source

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)setData:(NSDictionary *)data
{
    advertising = data[@"article"];
    contact     = data[@"article"][@"contact"];
}

- (IBAction)publishButtonOnTap:(id)sender
{
    NSMutableDictionary *appointment = [NSMutableDictionary new];

    appointment[@"userName"]        = contact[@"userName"];
    appointment[@"indexOfSaleTags"] = self.formValues[@"indexOfSaleTags"];
    appointment[@"startTime"]       = self.formValues[@"startTime"];
    appointment[@"address"]         = self.formValues[@"address"];
    appointment[@"message"]         = self.formValues[@"message"];
    appointment[@"advertisingId"]   = advertising[@"id"];
    if ([self.formValues[@"signature"] isEqual:[NSNull null]])
    {
    }

    AppointmentManager *appointmentManager = [AppointmentManager new];

    ResultSet *rs = [appointmentManager createAppointment:appointment];

    if ([self verifyResutSet:rs])
    {
        [self.view endEditing:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
